import React, { useState } from 'react'
import { Button, Form, Input, Row, Col, Card, Select } from 'antd';
import TableList from './TableList';
const Batchgeneration = () => {






  const [form] = Form.useForm();



  const [dataSource, setDataSource] = useState([])


  const Generate = () => {
    setDataSource([])

    form.validateFields().then(values => {
      // 一次性处理所有逻辑 性能最好 循环速度块 
      const optimizeLoop = () => {
        const excludeSet = values.exclude
          ? new Set(values.exclude.split(',').map(d => d.trim()))
          : new Set();

        const start = parseInt(values.start);
        const end = parseInt(values.end);
        const areaPrefix = values.area;

        const newEntries = [];

        for (let i = start; i <= end; i++) {
          const lastDigit = i % 10;

          if (excludeSet.has(lastDigit.toString())) continue;

          // 避免在循环中重复创建相同的字符串
          // newEntries.push({
          //   key: `${areaPrefix}-${i}`,
          //   area: `${areaPrefix}区`,
          //   Cabinet: i,
          //   type: values.type,
          //   variety: values.variety,
          //   region: values.region
          // });

          newEntries.push({
            "locker_name": i,
            "locker_number": i,
            "locker_product_class":  values.type,
            "locker_spec_name": "string",
            "locker_type": values.variety,
            "locker_user_type":values.region,
            "physical_zone": areaPrefix
          })
        }

        return newEntries;
      };

      const newEntries = optimizeLoop();
      setDataSource(prevData => [...prevData, ...newEntries]);


    }).catch(errorInfo => {
      console.log('表单验证失败：', errorInfo);
    });


  }


  return (
    <>
      <Card>
        <Form
          layout="inline"
          form={form}
        >
          <Row gutter={[16, 16]} >
            {/* 第一行 */}
            <Col span={4.8}>
              <Form.Item
                label="区号"

                labelAlign="left"
                name="area"
                initialValue={1}
                rules={[{ required: true, message: '请选择区号' }]}
              >
                <Select
                  placeholder="请选择区号"

                  style={{ width: '180px' }}
                  options={[
                    { value: 1, label: '1区' },
                    { value: 2, label: '2区' },
                    { value: 3, label: '3区' },

                  ]}
                />
              </Form.Item>
            </Col>

            <Col span={4.8}>
              <Form.Item
                label="开始柜号"
                initialValue={8001}
                labelAlign="left"
                name="start"
                rules={[{ required: true, message: '请输入开始柜号' }]}
              >
                <Input type='number' placeholder="请输入开始柜号" min={1} />
              </Form.Item>
            </Col>

            <Col span={4.8}>
              <Form.Item
                label="结束柜号"
                initialValue={8010}
                labelAlign="left"
                name="end"
                rules={[{ required: true, message: '请输入结束柜号' }]}
              >
                <Input type='number' placeholder="请输入结束柜号" min={1} />
              </Form.Item>
            </Col>

            <Col span={4.8}>
              <Form.Item
                label="柜类型"
                initialValue={1}
                labelAlign="left"
                name="type"
                rules={[{ required: true, message: '请选择柜类型' }]}
              >
                <Select

                  placeholder="请选择柜类型"
                  style={{ width: '180px' }}
                  options={[
                    { value: 1, label: '更衣柜' },
                    { value: 2, label: '鞋柜' },


                  ]}
                />
              </Form.Item>
            </Col>

            <Col span={4.8}>
              <Form.Item
                label="柜品种"
                initialValue={1}
                labelAlign="left"
                name="variety"
                rules={[{ required: true, message: '请选择柜品种' }]}
              >
                <Select

                  placeholder="请选择柜品种"
                  style={{ width: '180px' }}
                  options={[
                    { value: 1, label: '不带NFC' },
                    { value: 2, label: '将军不下马' },
                    { value: 3, label: '带NFC' },

                  ]}
                />
              </Form.Item>
            </Col>

            {/* 第二行 */}
            <Col span={4.8}>
              <Form.Item
                label="区域"
                initialValue={1}
                labelAlign="left"
                name="region"
                rules={[{ required: true, message: '请选择区域' }]}
              >

                <Select

                  placeholder="请选择区域"
                  style={{ width: '180px' }}
                  options={[
                    { value: 1, label: '男宾' },
                    { value: 2, label: '女宾' },


                  ]}
                />
              </Form.Item>
            </Col>

            <Col span={4.8}>
              <Form.Item
                label="排除数"

                labelAlign="left"
                name="exclude"
              >
                <Input placeholder="请输入排除数以 , 隔开" />
              </Form.Item>
            </Col>


          </Row>
        </Form>

        <div style={{ width: 240, display: 'flex', justifyContent: 'space-between', marginTop: 20, marginRight: 20, float: 'right' }}>
          <div>
            <Button type="primary" onClick={() => Generate()}>生成</Button>
          </div>
          <div>
            <Button type="primary">入库</Button>
          </div>
          <div><Button type="primary">返回</Button></div>
        </div>
      </Card>
      <br />
      <TableList dataSource={dataSource} setDataSource={setDataSource} />

    </>
  )
}

export default Batchgeneration